package com.ywjh.inter.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ywjh.inter.common.SystemConfig;
import com.ywjh.inter.entity.BindInsulinEntity;
import com.ywjh.inter.entity.BloodGlucoseEntity;
import com.ywjh.inter.entity.ProfileEntity;
import com.ywjh.inter.entity.UserFocusEntity;
import com.ywjh.inter.entity.UsersEntity;
import com.ywjh.inter.entity.UsersHXEntity;
import com.ywjh.inter.service.InsulinService;
import com.ywjh.inter.service.PatientDataService;
import com.ywjh.inter.service.UsersService;
import com.ywjh.inter.utils.PinyinUtils;
import com.ywjh.inter.utils.StrUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class Focus extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4665226458839823964L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html; charset=utf-8");

		JSONObject postData = StrUtil.getPostJSON(req);
		String act = postData.getString("act");
		JSONObject data = postData.getJSONObject("data");
		int r = 0;
		String e = "";
		JSONObject json = new JSONObject();
		switch (act) {
		case "list":
			int user_id = data.getInt("user_id");
			List<UserFocusEntity> list = UsersService.getInstance().listFocus(user_id);
			JSONArray array = new JSONArray();
			for (UserFocusEntity entity : list) {
				UsersEntity u = UsersService.getInstance().findUserById(entity.getFriend_id());
				UsersHXEntity hxEntity = UsersService.getInstance().findHXUserByUserId(entity.getFriend_id());
				if (u != null) {
					if (u.getIs_doc() == 0) {
						ProfileEntity profileEntity = UsersService.getInstance()
								.getProfileByUserId(entity.getFriend_id());
						JSONObject patientJson = new JSONObject();
						patientJson.put("head_url", SystemConfig.SERVER_DOMAIN + u.getHead_url());
						patientJson.put("loginname", u.getLoginname());
						patientJson.put("phone", u.getPhone());
						patientJson.put("hx_username", hxEntity.getUsername());
						patientJson.put("initial", "#");
						patientJson.put("is_doc", 0);
						if (profileEntity != null) {
							String py = PinyinUtils.toPinyinString(profileEntity.getName());
							if (py != null && py.length() > 1) {
								patientJson.put("initial", py.substring(0, 1).toUpperCase());
							}
							BindInsulinEntity insulin=InsulinService.getInstance().findInsulinByPatientId(user_id);
							JSONObject profileJson =JSONObject.fromObject(profileEntity.toJson(u,insulin));
							patientJson.put("profile", profileJson);
							BloodGlucoseEntity bloodGlucoseEntity = PatientDataService.getInstance()
									.findBloodBluLeastByUserId(profileEntity.getUser_id());
							if (bloodGlucoseEntity != null) {
								JSONObject bglu = new JSONObject();
								bglu.put("data", bloodGlucoseEntity.getData());
								bglu.put("check_time", StrUtil.getYYYYMMDDHHMISS(bloodGlucoseEntity.getCheck_time()));
								bglu.put("plan_id", bloodGlucoseEntity.getPlan_id());
								bglu.put("alarm",
										PatientDataService.getInstance().getAlarm(bloodGlucoseEntity.getUser_id(),
												bloodGlucoseEntity.getPlan_id(), bloodGlucoseEntity.getData()));
								patientJson.put("least_bglu", bglu);
							}
						}
						array.add(patientJson);
					}
				}
			}
			r = 1;
			json.put("d", array);
			break;
		case "set":
			user_id = data.getInt("user_id");
			JSONArray focus_array = data.getJSONArray("focus_array");
			boolean b = UsersService.getInstance().setFocus(user_id, focus_array);
			if (b) {
				r = 1;
			} else {
				e = "系统错误";
			}
			break;
		case "add":
			user_id = data.getInt("user_id");
			focus_array = data.getJSONArray("focus_array");
			b = UsersService.getInstance().addFocus(user_id, focus_array);
			if (b) {
				r = 1;
			} else {
				e = "系统错误";
			}
			break;
		case "delete":
			user_id = data.getInt("user_id");
			focus_array = data.getJSONArray("focus_array");
			b = UsersService.getInstance().deleteFocus(user_id, focus_array);
			if (b) {
				r = 1;
			} else {
				e = "系统错误";
			}
			break;
		}
		json.put("r", r);
		json.put("e", e);
		PrintWriter pw = resp.getWriter();
		pw.write(json.toString());
		pw.flush();
		pw.close();
	}
}
